<template>
	<view class="out">
		<navigator url="/pages/login/login">
			<button type="default">跳转到login</button>
		</navigator>
		<navigator url="/pages/requestDemo/requestDemo">
			<button type="default">跳转到requestDemo</button>
		</navigator>
		<navigator url="/pages/wallpaper/wallpaper">
			<button type="default">跳转到wallpaper</button>
		</navigator>
		<button @click="takePhoto">拍照</button>
		<image v-if="imagePath" :src="imagePath" mode="widthFix" @click="onPreview"></image>
	</view>
</template>

<script setup>
import {ref, reactive} from "vue"
//console.log("script setup")
import { pathToBase64, base64ToPath } from 'image-tools'
const file_base64 = ref("")
const obj = ref("")
const imagePath = ref("")
const server_token = ref("db140af0-c1b8-11ee-b115-0242ac110003")
// const imageStyle = reactive({
//       width: '750rpx' // 初始宽度占满父容器
//     });
const onPreview = function(){
	uni.previewImage({
		urls:[imagePath.value]
	})
}
const takePhoto = async () => {
uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['camera', 'album'],
					success: (res) => {
						const tempFilePaths = res.tempFilePaths;
						pathToBase64(tempFilePaths[0])
							.then(base64 => {
								let base64String = base64.substring(base64.indexOf(',') + 1)
								//console.log('base64String', base64String)
								file_base64.value = base64String
								uni.request({
									url:"https://rd.aitestgo.com/run/det",
									//url:"http://59.110.163.32:50052/run/det",
									data:{
										"data":[
											server_token.value,
											{
											"data":"data:image/jpeg;base64,"+base64String,
											"name":"p1.jpg",
											//"size":351604,
										}]},									
									method:"POST",
									success:res=>{
										//console.log(res.data.data)
										obj.value = res.data.data[0]
										imagePath.value=res.data.data[0]["imgUrl"]
										//let newShowHeight = res.data.data[0]["height"]*750/res.data.data[0]["width"]
										//imageStyle.height = `${newShowHeight}rpx`;
										//console.log("image_width:", res.data.data[0]["width"])
										//console.log("image_height:", res.data.data[0]["height"])										
									}
								})
							})
							.catch(error => {
								console.error(error)
							})
					}
				})
};

</script>

<style lang="scss" scoped>
.out{
	image{
		width:100%
	}
}
</style>
